home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / cenvid / sound.lib < prev    next >
Text File  |  1995-06-04  |  1KB  |  47 lines

  1. //*** Sound.lib - A few pc speaker sound routines
  2. //*** ver.1             
  3. //
  4. //*** sound(): play a tone for specified time
  5. // USAGE: void sound(int frequency,int duration)
  6. // WHERE: frequency: sound frequency in Hz
  7. //        duration: time to play in milliseconds
  8. //
  9. //*** StartTone(): start playing a tone
  10. // USAGE: void StartTone(int frequency)
  11. // WHERE: frequency: sound frequency in Hz
  12. //
  13. //*** StopTone(): stop playing any tone
  14. // USAGE void StopTone()
  15. //
  16.  
  17. sound(pFrequency,pDuration)
  18. {
  19.    StartTone(pFrequency);
  20.    suspend(pDuration);
  21.    StopTone();
  22. }
  23.  
  24. StartTone(pFrequency) // start 8253 programmable timer playing this frequency
  25. {
  26.    // determine counter to send to the 8253 programmable timer
  27.    #define  CHIP_RATE   1193180
  28.    counter = CHIP_RATE / pFrequency;
  29.    // program 8253
  30.    #define  COUNTER_REGISTER  0x42
  31.    #define  COMMAND_REGISTER  0x43
  32.    #define  SPEAKER_REGISTER  0x61
  33.    speaker = inport(SPEAKER_REGISTER);
  34.    if ( !(speaker & 0x3) ) {
  35.       speaker |= 0x3;
  36.       outport(SPEAKER_REGISTER,speaker);
  37.       outport(COMMAND_REGISTER,0xB6);
  38.    }
  39.    outport(COUNTER_REGISTER,counter & 0xFF);
  40.    outport(COUNTER_REGISTER,(counter >> 8) & 0xFF);
  41. }
  42.  
  43. StopTone()
  44. {
  45.    outport(SPEAKER_REGISTER,inport(SPEAKER_REGISTER) & 0xfc);
  46. }
  47.